package com.lpc.contentprovidertest;

import android.content.ContentResolver;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Uri uri = Uri.parse("content://com.lpc.contentprovidertest.MyContentProvider/update");
    private ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentResolver = getContentResolver();
        contentResolver.registerContentObserver(uri, true, observer);


    }

    public void click(View view) {

        contentResolver.update(uri, null, null, null);
    }

    private Handler handler = new Handler();

    private ContentObserver observer = new MyContentObserver(handler);

    class MyContentObserver extends ContentObserver {

        private final Handler handler;

        public MyContentObserver(Handler handler) {
            super(handler);
            this.handler = handler;
        }

        @Override
        public void onChange(boolean selfChange, final Uri uri) {
            super.onChange(selfChange, uri);

            handler.post(new Runnable() {
                @Override
                public void run() {

                    Toast.makeText(getApplicationContext(), "改变" + uri.getPath().toString(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
